package shortlink.common.biz.user;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import shortlink.common.convention.exception.ClientException;

import static shortlink.common.constant.RedisCacheConstant.USER_LOGIN_KEY;
import static shortlink.common.enums.UserErrorCodeEnum.USER_TOKEN_FAIL;
@RequiredArgsConstructor
@Component
public class UserInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String username = request.getHeader("username");
        if(StrUtil.isNotBlank( username)) {
            String userId=request.getHeader("userId");
            String realName=request.getHeader("realName");
            UserInfoDTO userInfoDTO = new UserInfoDTO(userId,username,realName);
            UserContext.setUser(userInfoDTO);
        }
        return true;


    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        UserContext.removeUser();
    }

}
